/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

// eslint-disable-next-line import/no-extraneous-dependencies
import { Request, Response } from 'express';
import { TableListItem, TableListParams } from '@/pages/Printer/data';
import { responseSuccess, responseRandom } from './utils';

// mock tableListDataSource
const genList = (page: number, limit: number) => {
  const tableListDataSource: TableListItem[] = [];
  const statusArray = ['online', 'offline'];
  for (let i = 0; i < limit; i += 1) {
    const index = (page - 1) * 10 + i;
    tableListDataSource.push({
      guid: `${new Date().getTime()}-${index}`,
      model: `PDD-${index}`,
      status: statusArray[Math.floor(Math.random() * 2) % 2],
      name: 'string',
      mark: 'string',
      manager: 'string',
      wechat: 'string',
      wifi: 'string',
      date: '2020-11-10 17:22',
    });
  }
  tableListDataSource.reverse();
  return tableListDataSource;
};

let tableListDataSource = genList(1, 100);

function getList(req: Request, res: Response, u: string) {
  let realUrl = u;
  if (!realUrl || Object.prototype.toString.call(realUrl) !== '[object String]') {
    realUrl = req.url;
  }
  const { page = 1, limit = 10 } = req.body;
  const params = (req.body as unknown) as TableListParams;

  let dataSource = [...tableListDataSource].slice(
    ((page as number) - 1) * (limit as number),
    (page as number) * (limit as number),
  );

  if (params.name) {
    dataSource = dataSource.filter((data) => data.name.includes(params.name || ''));
  }
  const result = {
    code: 0,
    msg: '',
    data: {
      data: dataSource,
      total: tableListDataSource.length,
      limit,
      page: parseInt(`${params.page}`, 10) || 1,
    },
  };

  return res.json(result);
}

export default {
  'POST /Api/AgentPrinter/AgentPrinter/agentList': getList,
  'POST /Api/AgentPrinter/AgentManager/clearPddAgentManager': (req: Request, res: Response) =>
    responseSuccess(req, res),
  'POST /Api/AgentPrinter/AgentPrinter/testPrint': responseRandom,
};
